<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="操作类型" prop="formId">
        <!-- <el-input
          v-model="queryParams.formId"
          placeholder="请输入操作id"
          clearable
          @keyup.enter="handleQuery"
        /> -->
        <el-select v-model="queryParams.formId" placeholder="请选择单位类别" clearable>
        <el-option v-for="dict in receipt_order_type" :key="dict.value" :label="dict.label" :value="dict.value"  @keyup.enter="handleQuery"></el-option>
        <el-option v-for="dict in shipment_order_type" :key="dict.value" :label="dict.label" :value="dict.value"  @keyup.enter="handleQuery"></el-option>
      </el-select>
      </el-form-item>
      <el-form-item label="物料名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库" prop="warehouseId">
                  <el-select @click="selectware" v-model="ware" placeholder="请选择仓库">
                    <el-option @click="seachware(dict.warehouseId)" v-for="dict in options"
                      :key="'key-' + dict.warehouseId" :label="dict.warehouseName"
                      :value="dict.warehouseId"></el-option>
                  </el-select>
      </el-form-item>
      <br>
      <el-form-item label="库区" prop="">
                  <el-select v-model="area" placeholder="请选择库区">
                    <el-option @click="" v-for="dict in areas" :key="'key-' + dict.areaNo" :label="dict.areaName"
                      :value="dict.areaNo"></el-option>
                  </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['wms:history:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="historyList" @selection-change="handleSelectionChange" :border="true">
      <el-table type="selection" width="55" align="center" />
      <!-- <el-table-column v-if="formType=1" label="操作类型" align="center" prop="receiptOrderType" >
        <template #default="scope" >
            <dict-tag :options="receipt_order_type" :value="scope.row.receiptOrderType" />
        </template>
      </el-table-column>
      <el-table-column v-else-if="formType=2" label="操作类型" align="center" prop="shipmentOrderType" >
        <template #default="scope" >
            <dict-tag :options="shipment_order_type" :value="scope.row.shipmentOrderType" />
        </template>
      </el-table-column> -->
      <el-table-column label="操作类型" align="center" prop="receiptOrderType" width="100">
          <template #default='scope'>
            <dict-tag :options="wms_receipt_type" :value="scope.row.receiptOrderType" />
          </template>
        </el-table-column>
      <el-table-column label="物料名称" align="center" prop="itemName" />
      <el-table-column label="所属仓库" align="center" prop="warehouseName" />
      <el-table-column label="所属库区" align="center" prop="areaName" />
      <el-table-column label="库存变化" align="center" prop="quantity" />
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['wms:history:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['wms:history:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="historyRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="操作id" prop="formId">
          <el-input v-model="form.formId" placeholder="请输入操作id" />
        </el-form-item>
        <el-form-item label="物料ID" prop="itemId">
          <el-input v-model="form.itemId" placeholder="请输入物料ID" />
        </el-form-item>
        <el-form-item label="库存变化" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入库存变化" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="删除标识" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标识" />
        </el-form-item>
        <el-table-column label="仓库" align="center" width="200" prop="warehouseId">
      <template #default="scope">
      <el-select v-model="scope.row.warehouseId" @change="onWarehouseChange(scope.row)" placeholder="请选择仓库">
      <el-option v-for="dict in options" :key="dict.warehouseId" :label="dict.warehouseName"
          :value="dict.warehouseId"></el-option>
      </el-select>
      </template>
      </el-table-column>
      <el-table-column label="库区" align="center" width="200" prop="areaId">
      <template #default="scope">
        <el-select v-model="scope.row.areaId" placeholder="请选择库区">
            <el-option v-for="dict in scope.row.itemAreas" :key="dict.areaNo" :label="dict.areaName"
                :value="dict.areaNo"></el-option>
        </el-select>
        </template>
      </el-table-column>
        <!-- <el-form-item label="所属仓库" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入所属仓库" />
        </el-form-item>
        <el-form-item label="所属库区" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入所属库区" />
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="History">
import { listHistory, getHistory, delHistory, addHistory, updateHistory } from "@/api/wms/history";
// import { selectAreaAll } from "@/api/wms/item";
import { selectList } from '@/api/wms/area';
import { listWarehouse } from '@/api/wms/warehouse'


const { proxy } = getCurrentInstance();
const { wms_receipt_type } = proxy.useDict("wms_receipt_type");
const historyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const ItemAreas = ref([])
const options = ref([]);
const ware = ref([]);
const area = ref([]);
const areas = ref([]);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    formId: null,
    formType: null,
    itemId: null,
    quantity: null,
    warehouseId: null,
    areaId: null,
    itemName:null,
    warehouseName:null,
    areaName:null,
    receiptOrderType:null,
    shipmentOrderType:null,
    warehouseNo:null,
    option:null,
  },
  rules: {
    delFlag: [
      { required: true, message: "删除标识不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

const handleChange = (value) => {
  console.log(value)
}

// /**级联选择器选择任意一级 */
// const props = {
//   checkStrictly: true,
// }
// const options = ref();
// getArea();


/** 字典*/
//const { receipt_order_type,shipment_order_type } = proxy.useDict("receipt_order_type","shipment_order_type");


/** 查询库存记录列表 */
function getList() {
  loading.value = true;
  if(queryParams.value.option !== null){
    // console.log(queryParams.value.option[0]);
    // console.log(queryParams.value.option[1]);
    queryParams.value.warehouseId = queryParams.value.option[0];
    queryParams.value.areaId = queryParams.value.option[1];
  }
  listHistory(queryParams.value).then(response => {
    console.log(response);
    historyList.value = response.rows;
    queryParams.value.warehouseId = null;
    queryParams.value.areaId = null;
    total.value = response.total;
    loading.value = false;
  });
}

function onWarehouseChange(row) {
  // 查询当前行所选仓库的库区
  selectList(row.warehouseId).then(response => {
    row.itemAreas = response.rows; // 仅更新当前行的 itemAreas
    if(row.API=undefined){
      row.areaId = null; // 重置选中的库区
    }
    console.log(row.itemAreas);
    
  });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    formId: null,
    formType: null,
    itemId: null,
    quantity: null,
    remark: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    warehouseId: null,
    areaId: null
  };
  proxy.resetForm("historyRef");
}


//查询仓库
function selectware() {
  listWarehouse().then(response => {
    console.log(response);
    options.value = response.rows
  })
}


//查询库区
function seachware(id) {
  selectList(id).then(response => {
    area.value = null
    areas.value = response.rows
  })
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加库存记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getHistory(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改库存记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["historyRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateHistory(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addHistory(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除库存记录编号为"' + _ids + '"的数据项？').then(function() {
    return delHistory(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/history/export', {
    ...queryParams.value
  }, `history_${new Date().getTime()}.xlsx`)
}

getList();
</script>
